import numpy as np
import cv2 as cv
import os

# 使用绝对路径访问视频文件
video_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'test_datas', 'test.mp4')
print(f"尝试打开视频文件: {video_path}")
print(f"文件是否存在: {os.path.exists(video_path)}")

cap = cv.VideoCapture(video_path)

# 检查视频是否成功打开
if not cap.isOpened():
    print(f"无法打开视频文件: {video_path}")
    exit()

print(f"视频宽度: {int(cap.get(cv.CAP_PROP_FRAME_WIDTH))}")
print(f"视频高度: {int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))}")
print(f"视频帧率: {cap.get(cv.CAP_PROP_FPS)}")

while cap.isOpened():
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    cv.imshow('frame', gray)
    if cv.waitKey(25) == ord('q'):
        break

cap.release()
cv.destroyAllWindows()